﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Kooboo.Web.Mvc;
using Kooboo.IoC;
using Kooboo.Commerce;

namespace Kooboo.Web.Areas.Commerce
{
    public static class SelectableListEx
    {
        #region Category
        public static SelectableList ToSelectableList(this IEnumerable<ICategory> categories, IEnumerable<ICategory> selecteds = null)
        {

            List<SelectListItem> items = new List<SelectListItem>();


            foreach (var i in categories.OrderBy(i => i.Name))
            {
                items.AddRange(BuildSelectListItems(i, selecteds, "·"));
            }

            return new SelectableList(items);
        }

        static List<SelectListItem> BuildSelectListItems(ICategory item, IEnumerable<ICategory> selecteds, string indent)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = indent + item.Name, Value = item.Id.ToString(), Selected = selecteds == null ? false : selecteds.Any(i => i.Id == item.Id) });

            foreach (var i in item.Children.OrderBy(i => i.Name))
            {
                items.AddRange(BuildSelectListItems(i, selecteds, indent + "·"));
            }


            return items;
        }

        #endregion

        #region Taxes
        public static SelectableList ToSelectableList(this IEnumerable<ITax> list, ITax selected = null)
        {
            return SelectableList.Build(list, i => i.Id, i => i.Name, selected);

        }
        #endregion

        #region IBrand
        public static SelectableList ToSelectableList(this IEnumerable<IBrand> list, IBrand selected = null)
        {
            return SelectableList.Build(list, i => i.Id, i => i.Name, selected);

        }
        #endregion

        #region ICountry
        public static SelectableList ToSelectableList(this IEnumerable<ICountry> list, ICountry selected = null)
        {
            return SelectableList.Build(list, i => i.CountryCode, i => i.CountryName, selected);
        }

        public static SelectableList ToSelectableList(this IEnumerable<ICountry> list, string code)
        {
            if (!String.IsNullOrEmpty(code))
            {
                var selected = ObjectContainer.CreateInstance<ICountry>();
                selected.CountryCode = code;

                return list.ToSelectableList(selected);
            }
            else
            {
                return list.ToSelectableList();
            }
        }
        #endregion

        #region IAddress
        public static SelectableList ToSelectableList(this IEnumerable<IAddress> list, IAddress selected = null)
        {
            return SelectableList.Build(list, i => i.Id, i => i.FirstName + " " + i.LastName, selected);
        }
        #endregion

        #region Shipping
        public static SelectableList ToSelectableList(this IEnumerable<IShipping> list, IShipping selected = null)
        {
            return SelectableList.Build(list, i => i.Id, i => i.Name, selected);
        }

        public static SelectableList ToSelectableList(this IEnumerable<IShipping> list, int id)
        {
            var selected = ObjectContainer.CreateInstance<IShipping>();
            selected.Id = id;

            return list.ToSelectableList(selected);
        }
        #endregion

        #region ProductType
        public static SelectableList ToSelectableList(this IEnumerable<IProductType> list, params IProductType[] selecteds)
        {
            return SelectableList.Build(list, i => i.Id, i => i.Name, selecteds);
        }
        #endregion

        #region SupportStatus
        public static SelectableList ToSelectableList(this SupportStatus status)
        {
            var names = Enum.GetNames(typeof(SupportStatus));
            var values = Enum.GetValues(typeof(SupportStatus));

            var list = new List<KeyValuePair<int, string>>();

            int i = 0;

            foreach (var value in values)
            {
                list.Add(new KeyValuePair<int, string>((int)value, names[i]));
                i++;
            }

            var selected = new KeyValuePair<int, string>((int)status, status.ToString());


            return SelectableList.Build(list, m => m.Key, m => m.Value, selected);
        }
        #endregion
    }
}